﻿#ifndef CHILDEDIT_H
#define CHILDEDIT_H

#include <QTextEdit>
#include <QPlainTextEdit>
#include "codeeditor.h"
#include <QFileSystemWatcher>
#include "highlighter.h"
#include <QStack>
#include <QCompleter>
#include "loopque.h"
#include <QStringListModel>
#include <QInputMethodEvent>
#define fileWatcherEnable 1

class ChildEdit : public CodeEditor
{
    Q_OBJECT
public:
    explicit ChildEdit(QWidget *parent = 0);
    ~ChildEdit();
    void newFile();                         //新建文件
    bool save();                            //保存文件
    bool saveAs();                          //另存为
    bool loadFile(const QString &fileName); //加载文件
    QString currentFile(){return curFile;}  //返回当前文件路径
    QString getCurrentFileFromInfo();       //返回当前文件名
    void setupHighLightTool(bool open);     //设置语法高亮
    void gotoLine(int lineNum);             //转到行
    void positisonMove(bool dir);           //前进后退
    void completerInit();                   //自动补全功能初始化
    void setCompleter(QCompleter *c);       //设置自动补全
    QCompleter *completer() const;
    QString textUnderCursor() const;        //获得光标所在的词
protected:
    void closeEvent(QCloseEvent *event);
    void contextMenuEvent(QContextMenuEvent *e);
    bool eventFilter(QObject *obj, QEvent *event);
    void focusInEvent(QFocusEvent *e);
private:
    static int sequenceNumber;
    QString curFile;                        //保存当前文件路径
    bool isUntitled;                        //当前文件是否被保存到硬盘上的标志

    bool saveFile(const QString &fileName);
    void setCurrentFile(const QString &fileName);
    bool maybeSave();                       //是否需要保存

    QFileSystemWatcher fsWatcher;           //文件监视器（监视是否外部修改）
    bool saveMyself;                        //自身保存标志

    Highlighter highlighter;                //语法高亮器

    QCompleter *c;                          //自动补全
    QStringListModel *string_list_model;
    QStringList word_list;                  //关键字列表

    loopQue positionQue;                    //位置栈

signals:
    void sig_fileClose(QString path);

public slots:
    void documentWasModified_SLOT();        //文件被修改处理槽
    void documentWatcher_Slot(const QString &path);    //文件被外部修改处理槽
    void positionChange_Slot();
    void insertCompletion_Slot(const QString &completion);
    void keyPressEvent(QKeyEvent *e);

};

#endif // CHILDEDIT_H
